package cn.stylefeng.guns.sys.modular.user.service.impl;

import cn.stylefeng.guns.sys.modular.dict.entity.SysDictData;
import cn.stylefeng.guns.sys.modular.dict.entity.SysDictType;
import cn.stylefeng.guns.sys.modular.dict.service.SysDictDataService;
import cn.stylefeng.guns.sys.modular.dict.service.SysDictTypeService;
import cn.stylefeng.guns.sys.modular.role.entity.SysRole;
import cn.stylefeng.guns.sys.modular.role.entity.SysRoleMenu;
import cn.stylefeng.guns.sys.modular.role.service.SysRoleMenuService;
import cn.stylefeng.guns.sys.modular.role.service.SysRoleService;
import cn.stylefeng.guns.sys.modular.user.entity.SysTenant;
import cn.stylefeng.guns.sys.modular.user.mapper.SysTenantMapper;
import cn.stylefeng.guns.sys.modular.user.service.SysTenantAsynDataService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date: 2023/12/26 13:40
 */
@Service
public class SysTenantAsynDataServiceImpl implements SysTenantAsynDataService {
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysDictTypeService dictTypeService;

    @Autowired
    private SysDictDataService dictDataService;

    @Autowired
    private SysTenantMapper sysTenantMapper;

    @Override
    @Async("sendToAlias")
    @Transactional(rollbackFor = Exception.class)
    public void asynPlatformData(Long tenantId) {
        try {
            //创建租户字典表数据 同步平台数据
            List<SysDictType> list = dictTypeService.list(new QueryWrapper<SysDictType>().lambda().eq(SysDictType::getTenantId, 1));
            for (SysDictType dictType : list) {
                List<SysDictData> dictDataList = dictDataService.list(new QueryWrapper<SysDictData>().lambda()
                        .eq(SysDictData::getTypeId, dictType.getId()));
                dictType.setId(null);
                dictType.setTenantId(tenantId);
                dictTypeService.save(dictType);
                for (SysDictData dictDate : dictDataList) {
                    dictDate.setTypeId(dictType.getId());
                    dictDate.setId(null);
                    dictDate.setTenantId(tenantId);
                }
                dictDataService.saveBatch(dictDataList);
            }

            //同步角色
            List<SysRole> sysRoleList = sysRoleService.list(new QueryWrapper<SysRole>().lambda().eq(SysRole::getTenantId, 1).eq(SysRole::getIsSync, 2));
            Map<Long, Long> roleMap = new HashMap<>();
            List<SysRoleMenu> sysRoleMenuList = new ArrayList<>();
            for (SysRole sysrole : sysRoleList) {
                Long oldRoleId = sysrole.getId();
                List<Long> roleMenuIdList = sysRoleMenuService.getRoleMenuId(sysrole);
                sysrole.setId(null);
                sysrole.setTenantId(tenantId);
                sysRoleService.save(sysrole);
                roleMap.put(oldRoleId, sysrole.getId());
                //角色菜单
                for (Long menuId : roleMenuIdList) {
                    SysRoleMenu sysRoleMenu = new SysRoleMenu();
                    sysRoleMenu.setRoleId(sysrole.getId());
                    sysRoleMenu.setMenuId(menuId);
                    sysRoleMenu.setTenantId(tenantId);
                    sysRoleMenuList.add(sysRoleMenu);
                }
            }
            sysRoleMenuService.saveBatch(sysRoleMenuList);

            //菜单 sys_role_menu 同步
            List<SysRoleMenu> sysRoleMenus = sysRoleMenuService.getBaseMapper().selectList(new QueryWrapper<SysRoleMenu>().lambda().eq(SysRoleMenu::getTenantId, 1));
            for (SysRoleMenu sysRoleMenu : sysRoleMenus) {
                sysRoleMenu.setId(null);
                sysRoleMenu.setTenantId(tenantId);
            }
            sysRoleMenuService.saveBatch(sysRoleMenus);

            //将租户创建状态修改为创建成功
            SysTenant sysTenant = sysTenantMapper.selectById(tenantId);
            sysTenant.setCreateStatus(2);
            sysTenantMapper.updateById(sysTenant);
        } catch (Exception e) {
            //将租户创建状态修改为创建失败
            SysTenant sysTenant = sysTenantMapper.selectById(tenantId);
            sysTenant.setCreateStatus(3);
            sysTenantMapper.updateById(sysTenant);
            throw new RuntimeException(e);
        }
    }
}
